package com.nicolasprof.profgallery.effects {
	import gs.TweenLite;

	import com.nicolasprof.profgallery.Engine;
	import com.nicolasprof.profgallery.effects.Effect;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.PixelSnapping;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	/**
	 * @author Nicolas Prof
	 */
	public class DoorsEffect extends Effect {
		public function DoorsEffect() {
			super();
		}
		
		private function applyMatrix($door : Sprite, $matrix : Matrix) : void {
			$door.transform.matrix = $matrix;
		}

		override public function build(...$layers) : void {
			var bitmap : Bitmap = $layers[0];
			var width : Number = bitmap.width;
			var height : Number = bitmap.height;
			var mask : Sprite = $layers[1];
			var config: XML = getConfig();
			var count: int = config.@countX;
			var time : Number = config.@time;
			
			var segWidth : Number = Math.ceil(width / count);
			var rebro : Number = (segWidth * 0.1) >> 0;
			var bmd : BitmapData = bitmap.bitmapData;
			var i: int = 0;
			var parent : DisplayObjectContainer = bitmap.parent;
			var index : int = parent.getChildIndex(bitmap);
			var posX: int;
			parent.removeChild(bitmap);
			
			for(i; i < count; ++i){
				posX = i * segWidth;
				
				var matrix : Matrix = new Matrix(0,1);
				var side : Shape = drawSide(rebro, height);
				var color : ColorTransform = new ColorTransform(1,1,1,1, -75, -75, -75);
				var door : Sprite = new Sprite();
				var segData : BitmapData = new BitmapData(segWidth, height);
				segData.copyPixels(bmd, new Rectangle(posX, 0, segWidth, height), new Point(0, 0));
				
				door.addChild(new Bitmap(segData, PixelSnapping.AUTO, true));
				parent.addChildAt(door, index);
				parent.addChild(side);
				
				door.x = matrix.tx = posX + rebro;
				side.x = posX;
				side.visible = false;
				door.transform.colorTransform = color;
				door.transform.matrix = matrix;
								
				TweenLite.to(matrix, time, {a:1, b:0, tx: posX,  delay:i*time, onUpdate:applyMatrix, onUpdateParams:[door, matrix]});
				TweenLite.to(door, time, {colorTransform:{redOffset:0, greenOffset:0, blueOffset:0}, delay:i*time});
				TweenLite.to(side, time, {colorTransform:{redOffset:-125, greenOffset:-125, blueOffset:-125}, visible:true, scaleX:0, delay:i*time});
			}
			
			bmd.dispose();

			mask.graphics.beginFill(0xFF0000);
			mask.graphics.drawRect(0, 0, width, height);
			mask.graphics.endFill();
			
			parent.mask = mask;
		}
		
		private function drawSide($width : Number, $height: Number) : Shape {
			var shape : Shape = new Shape();
			shape.graphics.beginFill(0xB7B7B7);
			shape.graphics.drawRect(0, 0, $width, $height);
			shape.graphics.endFill();
			return shape;
		}

		override protected function getConfig() : XML {
			var config : XML = Engine.CONFIG_XML;
			return config.effects.doors[0];
		}
	}
}
